package com.qingjiang.projectschedule.controller;

import com.qingjiang.projectschedule.pojo.UserInfo;
import com.qingjiang.projectschedule.service.ManageDeptService;
import com.qingjiang.projectschedule.service.UserInfoService;
import com.qingjiang.projectschedule.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("userinfo")
@CrossOrigin
public class UserInfoController {
    @Autowired
    private UserInfoService infoService;

    @Autowired
    private ManageDeptService manageDeptService;

    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token) {
        return infoService.getUserInfo(token);
    }

    @GetMapping("getUserInfoList")
    public Result getUserInfoList() {
        return infoService.getUserInfoList();
    }

    @PostMapping("updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfo userInfo) {
        UserInfo oriUser = infoService.getById(userInfo.getUid());
        if (userInfo.getRoleid() != 3 && !Objects.equals(userInfo.getDeptid(), oriUser.getDeptid())) {
            if (oriUser.getRoleid() == 3) {
                manageDeptService.deleteLeaderConfig(oriUser.getUid(), -1);
            }
        }
        if (userInfo.getRoleid() == 3 && oriUser.getRoleid() != 3) {
            manageDeptService.deleteLeaderConfig(oriUser.getUid(), oriUser.getDeptid());
        }

        return infoService.updateUserInfo(userInfo);
    }

    @GetMapping("getUserInfoListByRole")
    public Result getUserInfoListByRole(@Param("roleId") Integer roleId) {
        return infoService.getUserInfoListByRole(roleId);
    }

    @PostMapping("insertUserInfo")
    public Result insertUserInfo(@RequestBody UserInfo userInfo) {
        return infoService.insertUserInfo(userInfo);
    }
}
